<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      table {
        width: 500px;
        margin: 100px auto;
        border-collapse: collapse;
        text-align: center;
      }

      td,
      th {
        border: 1px solid #333;
      }

      thead tr {
        height: 40px;
        background-color: #ccc;
      }
    </style>
  </head>
  <body>
    <table collspacing="0">
      <thead>
        <tr>
          <th>姓名</th>
          <th>科目</th>
          <th>成绩</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
    <script>
      var dates = [
        {
          name: "许晨阳",
          subject: "javascript",
          score: 100,
        },
        {
          name: "弘历",
          subject: "javascript",
          score: 99,
        },
        {
          name: "郭少",
          subject: "javascript",
          score: 98,
        },
        {
          name: "张松涛",
          subject: "javascript",
          score: 100,
        },
      ];
      //往tbody里创建行 有几个人(通过数组长度)我们就创建几行
      var tbody = document.querySelector("tbody");
      for (var i = 0; i < dates.length; i++) {
        var tr = document.createElement("tr");
        tbody.appendChild(tr);
        //行里创建单元格
        for (var k in dates[i]) {
          var td = document.createElement("td");
          //把对象里面的属性值给td
          td.innerHTML = dates[i][k];
          tr.appendChild(td);
        }
        // 3. 创建有删除2个字的单元格
        var td = document.createElement("td");
        td.innerHTML = '<a href="javascript:;">删除 </a>';
        tr.appendChild(td);
      }
      var as = document.querySelectorAll("a");
      for (var i = 0; i < as.length; i++) {
        as[i].onclick = function () {
          tbody.removeChild(this.parentNode.parentNode);
        };
      }
      // for(var k in obj) {
      //     k 得到的是属性名
      //     obj[k] 得到是属性值
      // }
    </script>
  </body>
</html>
